package business.sistema;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

public class Properties 
{
	private static String PROPERTIES = "properties";
	private static String MAPEAMENTO = "mapeamento";
	private static String URL = "";
	private static String LOGO_EMPRESA = "";
	private static ArrayList<String> mapeamento;
	
	public static String URL() throws Exception
	{		
		BufferedReader arquivo = new BufferedReader(new FileReader(PROPERTIES));
		String line = "";
		
		while((line = arquivo.readLine()) != null)
		{
			if(line.substring(0, 3).equals("URL"))
			{
				URL = line.substring(5);
				arquivo.close();
				break;
			}
		}
		
//		URL = "jdbc:postgresql://localhost:5432/scev";
		
		return URL;
	}
	
	public static String LOGO_EMPRESA() throws Exception
	{
		BufferedReader arquivo = new BufferedReader(new FileReader(PROPERTIES));

		String line = "";
		
		while((line = arquivo.readLine()) != null)
		{
			if(line.substring(0, 12).equals("LOGO_EMPRESA"))
			{
				LOGO_EMPRESA = line.substring(14);
				arquivo.close();
				break;
			}
		}
		
		return LOGO_EMPRESA;
	}
	
	public static void setURL(String url)
	{
		URL = url;
	}
	
	public static void setLOGO_EMPRESA(String logoEmpresa)
	{
		LOGO_EMPRESA = logoEmpresa;
	}
	
	public static void UPDATE() throws Exception
	{
		BufferedWriter bufferWriter = new BufferedWriter(new FileWriter(PROPERTIES));
		String propriedades = "";
		
		propriedades += "URL: " + URL() + "\n";
		
		propriedades += "LOGO_EMPRESA: " + LOGO_EMPRESA() + "\n";
		
		bufferWriter.write(propriedades);
		bufferWriter.close();
	}
	
	public static ArrayList<String> MAPEAMENTO() throws Exception
	{		
		if(mapeamento == null)
		{
			mapeamento = new ArrayList<String>();
			
			BufferedReader arquivo = new BufferedReader(new FileReader(MAPEAMENTO));
			
			String line = "";
			
			while((line = arquivo.readLine()) != null)
			{
				mapeamento.add(line);
			}
			
			arquivo.close();
		}
		
		return mapeamento;
	}
}
